/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ik.RestauranteBar.SMO.Manejadores;

import java.util.List;
import javax.persistence.Query;
import org.ik.RestauranteBar.SMO.Encapsulados.Perfil;
import org.ik.RestauranteBar.SMO.Manejadores.Conectores.ConexionMySQL;
        
/**
 *
 * @author Malkavian
 */
public class ManejadorPerfiles extends ConexionMySQL {

    private static ManejadorPerfiles manager;

    public static ManejadorPerfiles getInstance() {
        if (manager == null) {
            manager = new ManejadorPerfiles();
        }
        return manager;
    }

    private ManejadorPerfiles() {
    }

    public List<Perfil> getPerfiles() {
        List<Perfil> clientes = null;

        try {
            Query query = getEntity().createNamedQuery("Perfil.findAll");
            clientes = query.getResultList();
        } catch (Exception ex) {
        }

        return clientes;
    }

    public Perfil getPermiso(Integer idpermiso) {
        Perfil cliente = null;

        try {
            Query query = getEntity().createNamedQuery("Perfil.findByIdpermiso");
            query.setParameter("idpermiso", idpermiso);
            cliente = (Perfil) query.getSingleResult();
        } catch (Exception ex) {
        }

        return cliente;
    }

    public int maxPerfiles() {

        try {

            Query query = getEntity().createNativeQuery("select MAX(idpermiso) FROM permisos");
            Object deleted = query.getSingleResult();
            return Integer.parseInt(deleted.toString());
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return 0;
        }

    }

    public Boolean borrarPerfiles(Perfil perfil) {

        try {

            Query query = getEntity().createNativeQuery("delete FROM Perfil u WHERE u.idpermiso = ?");
            query.setParameter(1, perfil.getIdperfil());
            int deleted = query.executeUpdate();
            return true;
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return false;
        }

    }

    public Boolean insertarPerfiles(Perfil cliente) {

        try {

            getEntity().getTransaction().begin();
            getEntity().persist(cliente);
            getEntity().getTransaction().commit();
            return true;

        } catch (Exception ex) {

            return false;
        }

    }

    public Boolean actualizarPerfiles(Perfil cliente) {

        try {

            getEntity().getTransaction().begin();
            getEntity().merge(cliente);
            getEntity().getTransaction().commit();
            return true;

        } catch (Exception ex) {

            return false;
        }

    }
}